<?php
/*
 * question控制器  
 * Tobey
 * */
require_once "baseController.class.php";
use Qiniu\Auth;
class questionController extends baseController{

    /* 七牛云，获取token */
    public function getToken() {
        $bucket = 'fangxwlab';
        $accessKey = 'z-gM_PuOaiEit8YZsXVk-6qYjEP1aOufk8dLH70f';
        $secretKey = 'lNcs2XExAv6szTpThRZ10LjV3kF5rkCMUKVivpBY';

        $auth = new Auth($accessKey, $secretKey);
        $upToken = $auth->uploadToken($bucket);

        $ret = array('uptoken' => $upToken);
        $this->ajaxReturn($ret);
        //echo json_encode($ret);
    }

	/**
     * 显示某张试卷的所有题目
     * get    展示模版
     * ajax   qus_id   获取信息
     * */
	public function question() {
		if(IS_GET) {
			$this->display();
		} else if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"])){
            session_start();
            $state = $_SESSION["isTeacher"];
            session_write_close();
            if($state == "true"){           //教师身份
        	    $data = $this->quizPower();            //用于检测权限的
                if ($data["status"] == 1) {
                    $data = $this->message(1,"获取试卷题目成功!",D()->table(array("question"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->field(array("que_id", "que_type", "que_content", "que_num", "que_item1", "que_item2", "que_item3", "que_item4", "que_item5",
                       "que_item6", "que_item7", "que_item8", "que_answer", "que_score"))->order("que_num")->select());
                }
            } else{     //学生身份
                $data = $this->stuQuizPower();
                if($data["status"] == 1){
                    $sql = D();
                    $quiz = $sql->table(array("question_set"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->field(array("qus_status"))->find();
                    session_start();
                    $stu_id = $_SESSION["id"];
                    session_write_close();
                    if($quiz["qus_status"] == 2){
                        $result = $sql->table(array("student_result"=>""))->where(array("qus_id"=>$_POST["qus_id"],"stu_id"=>$stu_id))->field(array("stu_score"))->find();
                        if($result){
                            $d["stu_score"] = $result["stu_score"];
                        } else{
                            $d["stu_score"] = "未交";
                        }
                        $field = array("que_id", "que_type", "que_content", "que_num", "que_item1", "que_item2", "que_item3", "que_item4", "que_item5",
                           "que_item6", "que_item7", "que_item8","que_answer", "que_score");
                    } else{
                        $d["stu_score"] = "正在测试中";
                        $field = array("que_id", "que_type", "que_content", "que_num", "que_item1", "que_item2", "que_item3", "que_item4", "que_item5",
                           "que_item6", "que_item7", "que_item8","que_score");
                    }
                    $ques = $sql->table(array("question"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->field($field)->order("que_num")->select();
                    $ques_len = count($ques);
                    $table = array("student_question"=>"");
                    $field = array("stu_answer");
                    for($i = 0;$i<$ques_len;$i++){
                        $result = $sql->table($table)->where(array("que_id"=>$ques[$i]["que_id"],"stu_id"=>$stu_id))->field($field)->find();
                        if($result){
                            $ques[$i]["stu_answer"] = $result["stu_answer"];
                        } else{
                            $ques[$i]["stu_answer"] = null;
                        }
                    }
                    $d["ques"] = $ques;
                    $data = $this->message(1,"获取试卷题目成功!",$d);
                    unset($sql,$quiz,$field,$ques,$ques_len,$stu_id,$d);
                }
            }
            $this->ajaxReturn($data);
            unset($data);
        }
    }

	/**
     * 为某张试卷添加题目
     * ajax
     * */
	public function add(){
		if(IS_AJAX && IS_LOCAL && isset($_POST["qus_id"])) {
            $data = $this->quizPower();
            if($data["status"] == 1){
                $sql = D();
			     //获取排序号码
                $_POST["que_num"] = $sql->table(array("question"=>""))->where(array("qus_id"=>$_POST["qus_id"]))->count() + 1;
                if($_POST["que_type"] == 4){     //如果是简答题的话,本系统是不帮助修改的!因此分数是0
                    $_POST["que_score"] = 1;     //为了过自动验证,暂时改为1
                }
                if($sql->table(array("question"=>""))->check($_POST,1)){
                    if($_POST["que_type"] == 4){
                        $_POST["que_score"] = 0;     //恢复0
                    }
                    $result = $sql->table(array("question"=>""))->add($_POST);
                    if(is_numeric($result)){
                        $data = $this->message(1,"题目保存成功!",$result);
                    } else{
                        $data = $this->message(0,"题目保存失败!","");
                    }
                } else{
                    $data = $this->message(0,$sql->getCheckError(),"");
                }
            }
            $this->ajaxReturn($data);
            unset($data);
        }
    }

    /*
    题目交换题号
    ajax 本地  que_id1  que_id2
    */
    public function move(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["que_id1"],$_POST["que_id2"])){
            $sql = D();     //开启数据库连接
            $sql->startTrans();     //开启事务管理
            $table = array("question"=>"");
            $ques1 = $sql->table($table)->where(array("que_id"=>$_POST["que_id1"]))->field(array("que_num"))->find();
            $ques2 = $sql->table($table)->where(array("que_id"=>$_POST["que_id2"]))->field(array("que_num"))->find();
            if($ques1 && $ques2){
                if($ques1["que_num"] < $ques2["que_num"]){
                    if($sql->table($table)->where(array("que_num"=>array("between",array($ques1["que_num"],$ques2["que_num"]))))->field(array("que_num"))->setDec(1) && $sql->table($table)->where(array("que_id"=>$_POST["que_id1"]))->update(array("que_num"=>$ques2["que_num"]))){
                        $sql->commit();
                        $data = $this->message(1,"题目交换题号成功!","");
                    } else{
                        $sql->rollback();
                        $data = $this->message(0,"题目换位失败!","");
                    }
                } else{
                    if($sql->table($table)->where(array("que_num"=>array("between",array($ques2["que_num"],$ques1["que_num"]))))->field(array("que_num"))->setInc(1) && $sql->table($table)->where(array("que_id"=>$_POST["que_id1"]))->update(array("que_num"=>$ques2["que_num"]))){
                        $sql->commit();
                        $data = $this->message(1,"题目交换题号成功!","");
                    } else{
                        var_dump($sql->getSql());
                        $sql->rollback();
                        $data = $this->message(0,"题目换位失败!","");
                    }
                }
            } else{
                $data = $this->message(0,"错误操作!","");
            }
            $this->ajaxReturn($data);
            unset($data,$sql,$table,$ques1,$ques1);
        }
    }

    // /**
    //  * 题目上移
    //  * ajax   que_id
    //  * */
    // public function moveUp(){
    // 	if(IS_AJAX && IS_LOCAL && isset($_POST["que_id"])) {
    // 		$data = $this->questionPower();
    // 		if($data["status"] == 1) {
    // 			$sql = D();
    // 			$sql->startTrans();			//开启事务
    // 			$current = $sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->field(array("que_num","qus_id"))->find();
    //             if($current["que_num"] > 1){            //不是第一题才上移
    //                 if($sql->table(array("question"=>""))->where(array("qus_id"=>$current["qus_id"],"que_num"=>$current["que_num"]-1))->field(array("que_num"))->setInc(1) && $sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->field(array("que_num"))->setDec(1)) {      //修改两个值!!
    //                     $sql->commit();           //提交
    //                     $data = $this->message(1,"题目上移成功!","");
    //                 } else{
    //                     $sql->rollback();         //回滚
    //                     $data = $this->message(0,"题目上移失败!","");
    //                 }
    //             } else{
    //             	$data = new Message(0,"该题目已经是第一题!","");
    //             }
    //             unset($current,$sql);
    //         }
    //         $this->ajaxReturn($data);
    //         unset($data);
    //     }
    // }

    // /**
    //  * 题目下移
    //  * ajax   que_id
    //  * */
    // public function moveDown(){
    // 	if(IS_AJAX && IS_LOCAL && isset($_POST["que_id"])) {
    // 		$data = $this->questionPower();
    // 		if($data["status"] == 1) {
    // 			$sql = D();
    // 			$sql->startTrans();		//开启事务
    // 			$current = $sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->field(array("que_num","qus_id"))->find();
    //             if($current["que_num"] < $sql->table(array("question"=>""))->where(array("qus_id"=>$current["qus_id"]))->count()){     //可以下移的情况:当前的题号小于总的题目数量的
    //             	if($sql->table(array("question"=>""))->where(array("qus_id"=>$current["qus_id"],"que_num"=>$current["que_num"]+1))->field(array("que_num"))->setDec(1) && $sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->field(array("que_num"))->setInc(1)){
    //                     $sql->commit();           //提交
    //                     $data = $this->message(1,"题目下移成功!","");
    //                 } else{
    //                     $sql->rollback();     //回滚
    //                     $data = $this->message(0,"题目下移失败!","");
    //                 }
    //             } else{
    //             	$data = new Message(0,"该题目已经是最后一题!","");
    //             }
    //             unset($current,$sql);
    //         }
    //     } else{
    //     	$data = $this->message(0,"非法操作!!","");
    //     }
    //     $this->ajaxReturn($data);
    //     unset($data);
    // }

    /**
     * 删除题目
     * 特殊:题目删除要做出判断,如果是最后一题就可以直接删除,如果不是就要修改其他题目的编号!
     * ajax   que_id
     * */
    public function delete(){
    	if(IS_AJAX && IS_LOCAL && isset($_POST["que_id"])) {
    		$data = $this->questionPower();
    		if($data["status"] == 1) {
    			$sql = D();
                $sql->startTrans();			//开启事务
                $current = $sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->field(array("que_num","qus_id"))->find();
                $count = $sql->table(array("question"=>""))->where(array("qus_id"=>$current["qus_id"]))->count();
                if($current["que_num"] < $count){      //不是最后一题，要修改其他题目的num
                	if($sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->delete() && $sql->table(array("question"=>""))->where(array("qus_id"=>$current["qus_id"],"que_num"=>array(">",$current["que_num"])))->field(array("que_num"))->setDec(1)){
                        $sql->commit();       //提交
                        $data = $this->message(1,"题目删除成功!","");
                    } else {
                        $sql->rollback();     //回滚
                        $data = $this->message(0,"题目删除失败!","");
                    }
                } else{
                	if($sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->delete()){
                		$sql->commit();
                		$data = $this->message(1,"题目删除成功!","");
                	} else{
                		$data = $this->message(0,"题目删除失败!","");
                	}
                }
                unset($current,$count,$sql);
            }
        } else{
        	$data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        unset($data);
    }

    /**
     * 修改题目
     * ajax
     * */
    public function update(){
    	if (IS_AJAX && IS_LOCAL) {
    		$data = $this->questionPower();
    		if ($data["status"] == 1) {
    			$sql = D();
    			if($sql->table(array("question"=>""))->check($_POST,2)){
                    if($_POST["que_type"] == 4){
                        $_POST["que_score"] = 0;     //恢复0
                    }
                    if($sql->table(array("question"=>""))->where(array("que_id"=>$_POST["que_id"]))->update($_POST)){
                     $data = $this->message(1,"题目信息修改成功!","");
                 } else{
                     $data = $this->message(0,"题目信息没有任何变化,无需修改!","");
                 }
             } else{
                $data = $this->message(0,$sql->getCheckError(),"");
            }
            unset($sql);
        }
    } else{
      $data = $this->message(0,"非法操作!!","");
  }
  $this->ajaxReturn($data);
  unset($data);
}

	/**
  	 * 析构函数不用写任何内容也是可以的
  	 */
	public function __destruct(){}
}
